<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司编号" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入公司编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同号" prop="htCode">
        <el-input
          v-model="queryParams.htCode"
          placeholder="请输入合同号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属公司" prop="projectCompany">
        <el-input
          v-model="queryParams.projectCompany"
          placeholder="请输入所属公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投资人" prop="tzUser">
        <el-input
          v-model="queryParams.tzUser"
          placeholder="请输入投资人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投资总价" prop="totalPrice">
        <el-input
          v-model="queryParams.totalPrice"
          placeholder="请输入投资总价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:jcproject:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:jcproject:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:jcproject:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:jcproject:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jcprojectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目编号" align="center" prop="id" />
      <el-table-column label="公司编号" align="center" prop="deptId" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="合同号" align="center" prop="htCode" />
      <el-table-column label="所属公司" align="center" prop="projectCompany" />
      <el-table-column label="投资人" align="center" prop="tzUser" />
      <el-table-column label="投资总价" align="center" prop="totalPrice" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" width="260" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="toInfo(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="addInfo(scope.row)"
          >新增明细</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:jcproject:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:jcproject:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目管理对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司编号" prop="deptId">
              <el-input v-model="form.deptId" placeholder="请输入公司编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同号" prop="htCode">
              <el-input v-model="form.htCode" placeholder="请输入合同号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属公司" prop="projectCompany">
              <el-input v-model="form.projectCompany" placeholder="请输入所属公司" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="投资人" prop="tzUser">
              <el-input v-model="form.tzUser" placeholder="请输入投资人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投资总价" prop="totalPrice">
              <el-input v-model="form.totalPrice" placeholder="请输入投资总价" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 项目详情 -->
    <el-dialog  :title="titleXiang" :close-on-press-escape="false"	 :close-on-click-modal="false"  :show-close="false" :visible.sync="openXiang" width="1200px" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="xiangAdd"
            v-hasPermi="['base:projectinfo:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single2"
            @click="xiangUpdate"
            v-hasPermi="['base:projectinfo:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple2"
            @click="xiangDelete"
            v-hasPermi="['base:projectinfo:remove']"
          >删除</el-button>
        </el-col>
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="warning"-->
<!--            plain-->
<!--            icon="el-icon-download"-->
<!--            size="mini"-->
<!--            @click="handleExport"-->
<!--            v-hasPermi="['base:projectinfo:export']"-->
<!--          >导出</el-button>-->
<!--        </el-col>-->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList2"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="xiangList"  @selection-change="handleSelectionChange2">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键" align="center" prop="id" />
        <el-table-column label="公司编号" align="center" prop="deptId" />
        <el-table-column label="项目编号" align="center" prop="mainId" />
        <el-table-column label="项目名称" align="center" prop="projectName" />
        <el-table-column label="追加价格" align="center" prop="zjPrice" />
        <el-table-column label="追加时间" align="center" prop="zjTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.zjTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="追加人" align="center" prop="zjName" />
        <el-table-column label="备注" align="center" prop="renark" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="closeD">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改项目详情对话框 -->
    <el-dialog :close-on-click-modal="false" title="添加项目详情" :visible.sync="openXiangInfo" width="500px" append-to-body>
      <el-form ref="form2" :model="form2" :rules="rules2" label-width="80px">
<!--        <el-form-item label="公司编号" prop="deptId">-->
<!--          <el-input v-model="form2.deptId" placeholder="请输入公司编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="项目编号" prop="mainId">-->
<!--          <el-input v-model="form2.mainId" placeholder="请输入项目编号" />-->
<!--        </el-form-item>-->
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form2.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="追加价格" prop="zjPrice">
          <el-input v-model="form2.zjPrice" placeholder="请输入追加价格" />
        </el-form-item>
        <el-form-item label="追加时间" prop="zjTime">
          <el-date-picker clearable
                          v-model="form2.zjTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择追加时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="追加人" prop="zjName">
          <el-input v-model="form2.zjName" placeholder="请输入追加人" />
        </el-form-item>
        <el-form-item label="备注" prop="renark">
          <el-input v-model="form2.renark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listJcproject, getJcproject, delJcproject, addJcproject, updateJcproject } from "@/api/base/jcproject";
// import { getProjectinfo } from "@/api/base/projectinfo";
import { listProjectinfo, getProjectinfo, delProjectinfo, addProjectinfo, updateProjectinfo } from "@/api/base/projectinfo";

export default {
  name: "Jcproject",
  data() {
    var validatePrice = (rule, value, callback) => {
      var reg = /^(0|[1-9]\d{0,17})(\.\d{1,2})?$/;
      if(!reg.test(value) && value!=undefined && value!=""){
        return callback(new Error('只能输入数值保留两位小数'));
      } else{
        callback();
      }
    };
    return {
      // 遮罩层
      loading: true,
      loading2:false,
      // 选中数组
      ids: [],
      ids2: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 非单个禁用
      single2: true,
      // 非多个禁用
      multiple2: true,
      // 总条数
      total: 0,
      total2: 0,
      // 项目管理表格数据
      jcprojectList: [],
      xiangList:[],
      // 弹出层标题
      title: "",
      titleXiang:'项目详情',
      // 是否显示弹出层
      open: false,
      openXiang:false,
      openXiangInfo:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        projectName: null,
        htCode: null,
        projectCompany: null,
        tzUser: null,
        totalPrice: null,
        res1: null,
        res2: null,
        res3: null
      },
      // 查询参数
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        mainId: null,
        projectName: null,
        zjPrice: null,
        zjTime: null,
        zjName: null,
        renark: null,
        res1: null,
        res2: null,
        res3: null
      },
      // 表单参数
      form: {},
      form2: {},
      // 表单校验
      rules: {
        deptId: [
          { required: true, message: "公司编号不能为空", trigger: "blur" }
        ],

      },
      rules2: {
        zjPrice: [
          { validator: validatePrice,trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //新增项目
    xiangAdd(){
      // this.reset2();
      this.openXiangInfo = true;
      // this.titleX = "添加项目详情";
    },
    xiangUpdate(row) {
      this.reset2();
      const id = row.id || this.ids2
      getProjectinfo(id).then(response => {
        this.form2 = response.data;
        this.openXiangInfo = true;
        // this.title = "修改项目详情";
      });
    },
    // 查看详情
    toInfo(row){
      this.form2.mainId = row.id
      listProjectinfo(row.id).then(res=>{
        this.openXiang = true
        this.xiangList = res.rows
        console.log(res,'resssssssssssssss')
      })
    },
    /** 查询项目管理列表 */
    getList() {
      this.loading = true;
      listJcproject(this.queryParams).then(response => {
        this.jcprojectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    closeD(){
      this.openXiang = false
      this.getList()
    },
    getList2() {
      this.loading2 = true;
      listProjectinfo(this.queryParams2).then(response => {
        this.xiangList = response.rows;
        this.total2 = response.total;
        this.loading2 = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel2(){
      this.openXiangInfo = false;
      this.reset2();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        projectName: null,
        htCode: null,
        projectCompany: null,
        tzUser: null,
        totalPrice: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        res1: null,
        res2: null,
        res3: null
      };
      this.resetForm("form");
    },
    // 表单重置
    reset2() {
      this.form2 = {
        id: null,
        deptId: null,
        mainId: null,
        projectName: null,
        zjPrice: null,
        zjTime: null,
        zjName: null,
        renark: null,
        res1: null,
        res2: null,
        res3: null
      };
      this.resetForm("form2");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 多选框选中数据
    handleSelectionChange2(selection) {
      this.ids2 = selection.map(item => item.id)
      this.single2 = selection.length!==1
      this.multiple2 = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getJcproject(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateJcproject(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addJcproject(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm2() {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          if (this.form2.id != null) {
            console.log(this.form2,'11')
            updateProjectinfo(this.form2).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openXiangInfo = false;
              this.getList2();
            });
          } else {
            console.log(this.form2,'22')
            addProjectinfo(this.form2).then(response => {

              this.$modal.msgSuccess("新增成功");
              this.openXiangInfo = false;
              this.getList2();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目管理编号为"' + ids + '"的数据项？').then(function() {
        return delJcproject(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    xiangDelete(row) {
      const ids = row.id || this.ids2;
      this.$modal.confirm('是否确认删除项目详情编号为"' + ids + '"的数据项？').then(function() {
        return delProjectinfo(ids);
      }).then(() => {
        this.getList2();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/jcproject/export', {
        ...this.queryParams
      }, `jcproject_${new Date().getTime()}.xlsx`)
    },
    addInfo(row){
      this.form2.mainId = row.id
      // this.disabled = true
      this.openXiangInfo = true
    }
  }
};
</script>
